package com.example.demo.module.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.develop.DevelopBomPoJo;
import com.example.demo.entity.pojo.purchase.PurchaseListPoJo;
import com.example.demo.entity.pojo.purchase.PurchaseOrderPoJo;
import com.example.demo.module.purchase.mapper.PurchaseListMapper;
import com.example.demo.module.purchase.service.PurchaseListService;
import com.example.demo.module.purchase.service.PurchaseOrderService;
import com.example.demo.utils.ChinesePinYinUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("purchaseListService")
public class PurchaseListServiceImpl extends BaseServiceImpl<PurchaseListMapper, PurchaseListPoJo> implements PurchaseListService {
    @Resource
    private PurchaseListMapper mapper;
    @Resource
    private PurchaseOrderService orderService;

    @Override
    public List<PurchaseListPoJo> findList(PurchaseListPoJo poJo, Date startTime, Date endTime) {
        return mapper.selectList(getLambda(poJo, startTime, endTime));
    }

    @Override
    public Boolean createBatchCid(List<PurchaseListPoJo> list, Long companyId) {

        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
        Date dt = new Date();
        String date = fmt.format(dt);

        //1.采购订单表
        PurchaseOrderPoJo orderPoJo = new PurchaseOrderPoJo();
        orderPoJo.setSubmitTime(dt);
        orderPoJo.setState(0l);
        orderPoJo.setCompanyId(companyId);
        String enterprise = ChinesePinYinUtil.getPinYinHeadChar(list.get(0).getEnterprise()).toUpperCase() + date;
        orderPoJo.setOrderNum(enterprise);
        orderPoJo.setEnterprise(list.get(0).getEnterprise());
        Long orderId = orderService.create(orderPoJo).getId();

        //2.采购清单表
        List<Long> ids = new ArrayList();
        list.forEach(l -> {
            l.setSubmitTime(dt);
            l.setCompanyId(companyId);
            l.setOrderId(orderId);
            create(l);
        });

        return true;
    }

    @Override
    public void update(PurchaseListPoJo poJo,PurchaseOrderPoJo pj) {
        mapper.updateById(poJo);
        //更新采购订单表状态
        orderService.updateById(pj);
    }

    private LambdaQueryWrapper<PurchaseListPoJo> getLambda(PurchaseListPoJo poJo, Date startTime, Date endTime) {
        Long id = poJo.getId();
        Long companyId = poJo.getCompanyId();
        Date createTime = poJo.getCreateTime();
        Date updateTime = poJo.getUpdateTime();
        Boolean isDelete = poJo.getIsDelete();
        String brand = poJo.getBrand();
        String name = poJo.getName();
        String spec = poJo.getSpec();
        Integer quantity = poJo.getQuantity();
        String unit = poJo.getUnit();
        String remarks = poJo.getRemarks();
        Long supplierId = poJo.getSupplierId();
        String supplierName = poJo.getSupplierName();
        Long taxRate = poJo.getTaxRate();
        Long orderId = poJo.getOrderId();
        String enterprise = poJo.getEnterprise();
        Long withVat = poJo.getWithVat();
        Long withoutVat = poJo.getWithoutVat();

        LambdaQueryWrapper<PurchaseListPoJo> wrapper = new LambdaQueryWrapper<PurchaseListPoJo>();
        wrapper
                .eq(id != null, PurchaseListPoJo::getId, poJo.getId())
                .eq(withoutVat != null, PurchaseListPoJo::getWithoutVat, poJo.getWithoutVat())
                .eq(withVat != null, PurchaseListPoJo::getWithVat, poJo.getWithVat())
                .eq(taxRate != null, PurchaseListPoJo::getTaxRate, poJo.getTaxRate())
                .eq(supplierName != null, PurchaseListPoJo::getSupplierName, poJo.getSupplierName())
                .eq(companyId != null, PurchaseListPoJo::getCompanyId, poJo.getCompanyId())
                .eq(createTime != null, PurchaseListPoJo::getCreateTime, poJo.getCreateTime())
                .eq(updateTime != null, PurchaseListPoJo::getUpdateTime, poJo.getUpdateTime())
                .eq(isDelete != null, PurchaseListPoJo::getIsDelete, poJo.getIsDelete())
                .like(supplierId != null, PurchaseListPoJo::getSupplierId, poJo.getSupplierId())
                .eq(brand != null, PurchaseListPoJo::getBrand, poJo.getBrand())
                .eq(name != null, PurchaseListPoJo::getName, poJo.getName())
                .eq(spec != null, PurchaseListPoJo::getSpec, poJo.getSpec())
                .eq(quantity != null, PurchaseListPoJo::getQuantity, poJo.getQuantity())
                .eq(unit != null, PurchaseListPoJo::getUnit, poJo.getUnit())
                .eq(enterprise != null, PurchaseListPoJo::getEnterprise, poJo.getEnterprise())
                .eq(remarks != null, PurchaseListPoJo::getRemarks, poJo.getRemarks())
                .like(orderId != null, PurchaseListPoJo::getOrderId, poJo.getOrderId())//采购订单编号
                .ge(startTime != null, PurchaseListPoJo::getSubmitTime, startTime)
                .le(endTime != null, PurchaseListPoJo::getSubmitTime, endTime);

        return wrapper;
    }
}